from DjangoSite.burndown.models import GraphData, Task
from django.utils.datetime_safe import datetime
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User

TASK_COMPLETED = 0
TASK_DELETED = 1

MSG_REQUIRE_LOGIN = "NOT_LOGGED_IN"
MSG_GENERAL_ERROR = "GENERAL_ERROR"

def userLogin(request, username, password):
    print "[userlogin] : username=" + username + ", password="  + password
    user = authenticate(username=username,password=password)
    if user is not None:
        login(request,user)
        return True
    return False

def userLogout(request):
    print "[userlogout]"
    logout(request)

def getGraphDataAll(request):
    print "[getGraphDataAll]"
    result = []
    date_cursor = None
    result_cursor = -1
    for i in GraphData.objects.all():
        d = i.date
        if d != date_cursor:
            date_cursor = d
            result_cursor += 1
            result.append({"date" : date_cursor, "graphData" : {}})
        result[result_cursor]["graphData"][i.person.username] = {"taskcount" : i.taskcount, "timecount" : i.timecount}
    return result

def getGraphDataToday(request):
    print "[getGraphDataToday]"
    return GraphData.objects.select_related().filter(date=datetime.today())

def getMyTaskAll(request):
    print "[getMyTaskAll]"
    user = request.user
    if (request.user.id==None):
        return MSG_REQUIRE_LOGIN
    return Task.objects.filter(person=user)

def getMyTaskToday(request):
    print "[getMyTaskToday]"
    user = request.user
    if (request.user.id==None):
        return MSG_REQUIRE_LOGIN
    return Task.objects.filter(person=user, enddate=None)

def addTask(request,begindate):
    print "[addTask] : begindate=" + begindate.__str__()
    user = request.user
    if (user.id==None):
        return MSG_REQUIRE_LOGIN
    Task(person=user,begindate=begindate).save()

def deleteTask(request,tid): 
    print "[deleteTask] : tid=" + tid.__str__()
    user = request.user
    if (user.id==None):
        return MSG_REQUIRE_LOGIN
    task = Task.objects.select_related().get(pk=tid)
    if (task==None or task.person_id!=user.id or task.enddate!=None):
        return MSG_GENERAL_ERROR
    Task.objects.filter(pk=tid).update(enddate=datetime.now(),endreason=TASK_DELETED)
    
def editTask(request,tid,name,time):
    print "[editTask] : tid=" + tid.__str__() + ", name=" + name + ", time=" + time
    user = request.user
    if (user.id==None):
        return MSG_REQUIRE_LOGIN
    task = Task.objects.select_related().get(pk=tid)
    if (task==None or task.person_id!=user.id or task.enddate!=None):
        return MSG_GENERAL_ERROR
    Task.objects.filter(pk=tid).update(name=name,time=time)

def completeTask(request,tid):
    print "[completeTask] : tid=" + tid
    user = request.user
    if (user.id==None):
        return MSG_REQUIRE_LOGIN
    task = Task.objects.select_related().get(pk=tid)
    if (task==None or task.person_id!=user.id or task.enddate!=None):
        return MSG_GENERAL_ERROR
    Task.objects.filter(pk=tid).update(enddate=datetime.now(),endreason=TASK_COMPLETED)
    